using System.ComponentModel;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Rendering;
using UnityEngine.UI;

public class UIManager : MonoBehaviour {

    public PlayerStateBar playerStateBar;

    [Header("事件监听")]
    public CharacterEventSO healthEvent;
    public SceneLoadEventSO unLoadedSceneEvent;     // 卸载场景的事件监听
    public VoidEventSO loadDataEvent;               // 保存数据的事件监听
    public VoidEventSO gameOverEvent;               // 游戏结束的事件监听
    public VoidEventSO backToMenuEvent;             // 返回到主菜单的事件监听

    [Header("组件")]
    public GameObject gameOverPanel;                // 游戏结束面板
    public GameObject restartBtn;                   // 重开按钮
    public GameObject mobileTouch;                  // 移动端触控
    public GameObject pausePanel;                   // 暂停面板
    public Button SettingButton;                    // 设置按钮

    private void Awake() {
        // 预编译，如果当前平台不是移动端，则不显示触控按钮
        #if UNITY_STANDALONE
            mobileTouch.SetActive(false);
        #endif
    }

    private void OnEnable() {
        healthEvent.OnEventRaised += OnHealthEvent;
        unLoadedSceneEvent.LoadRequestEvent += OnUnloadedSceneEvent;
        loadDataEvent.OnEventRaised += OnLoadDataEvent;
        gameOverEvent.OnEventRaised += OnGameOverEvent;
        backToMenuEvent.OnEventRaised += OnBackToMenuEvent;
        SettingButton.onClick.AddListener(OnSettingButtonClick);
    }

    private void OnDisable() {
        healthEvent.OnEventRaised -= OnHealthEvent; 
        unLoadedSceneEvent.LoadRequestEvent -= OnUnloadedSceneEvent;
        loadDataEvent.OnEventRaised -= OnLoadDataEvent;
        gameOverEvent.OnEventRaised -= OnGameOverEvent;
        backToMenuEvent.OnEventRaised -= OnBackToMenuEvent;
    }

    private void OnHealthEvent(Character character) {
        float persentage = character.curHealth / character.maxHealth;
        playerStateBar.OnHealthChange(persentage);

        playerStateBar.OnPowerChange(character);
    }

    private void OnUnloadedSceneEvent(GameSceneSO sceneToLoad, Vector3 arg1, bool arg2) {
        if (sceneToLoad.sceneType == SceneType.Location) {  // 如果当前加载的场景是游戏关卡，则需要显示出玩家状态栏
            playerStateBar.gameObject.SetActive(true);
        } else {
            playerStateBar.gameObject.SetActive(false);
        }
    }

    private void OnGameOverEvent() {
        gameOverPanel.SetActive(true);                              // 打开游戏结束面板
        EventSystem.current.SetSelectedGameObject(restartBtn);      // 默认选中重开
    }

    private void OnLoadDataEvent() {
        gameOverPanel.SetActive(false);
    }

    public void OnSettingButtonClick() {
        if (!pausePanel.activeSelf) {
            pausePanel.SetActive(true);
            Time.timeScale = 0;
        } else {
            pausePanel.SetActive(false);
            Time.timeScale = 1;
        }
    }

    private void OnBackToMenuEvent() {
        gameOverPanel.SetActive(false);
        pausePanel.SetActive(false);
        Time.timeScale = 1;
    }

}